﻿#region Copyright and License
/*==============================================================================
 *  Copyright (c) www.51mvc.com Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.51mvc.com/
 * Create By 小鱼(Leven)  - http://leven.com.cn
 * ==============================================================================*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Infancy.Core.Entities;

namespace Infancy.Core.Repository {
    /// <summary>
    /// 数据层基本接口的默认实现类,大部分实现类都继承该类
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <typeparam name="TContext"></typeparam>
    public abstract class RepositoryBase<TEntity, TContext> : IRepository<TEntity> where TEntity : IEntity {
        /// <summary>
        /// 抽象数据保存操作对象,比如封装Aef后改对象用来保存ObjectContext,封装Ado.Net保存IConnection
        /// 主要使用对象为子类
        /// </summary>
        protected RepositoryContext<TContext> Context {
            get;
            private set;
        }

        /// <summary>
        /// 该方法为Unity间接调用,初始化数据层操作对象
        /// </summary>
        /// <param name="context"></param>
        public RepositoryBase(RepositoryContext<TContext> context) {
            Context = context;
        }

        #region IRepository<TEntity> Members

        /// <summary>
        /// 插入一条数据
        /// </summary>
        /// <param name="entity">数据实体</param>
        public abstract void Insert(TEntity entity);

        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// <param name="entity">数据实体</param>
        public abstract void Update(TEntity entity);

        /// <summary>
        /// 删除指定的实体数据(以ID为准)
        /// </summary>
        /// <param name="entity">实体数据</param>
        public abstract void Delete(TEntity entity);

        /// <summary>
        /// 删除指定的数据
        /// </summary>
        /// <param name="id">ID信息</param>
        public abstract void Delete(long id);

        /// <summary>
        /// 统计数据库中该表(类型)数据的总量
        /// </summary>
        /// <returns></returns>
        public abstract int Count();

        /// <summary>
        /// 获取单条数据
        /// </summary>
        /// <param name="id">id主键信息</param>
        /// <returns></returns>
        public abstract TEntity GetSingle(long id);

        /// <summary>
        /// 获取该表(类型)的所有数据,一般用于小表(类型)
        /// </summary>
        /// <returns></returns>
        public abstract IList<TEntity> GetList();

        /// <summary>
        /// 创建一个该表(类型)对应的默认实体
        /// </summary>
        /// <returns></returns>
        public abstract TEntity Create();

        #endregion
    }
}
